package cn.enited.devtools.apiaggregator.controller;

import cn.enited.devtools.apiaggregator.consts.ErrorCode;
import cn.enited.devtools.apiaggregator.exception.BizException;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Optional;

public abstract class BaseController {

    HttpServletRequest getRequest() {
        return Optional.ofNullable(((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest())
                .orElseThrow(() -> new BizException(ErrorCode.ERROR));
    }

    HttpServletResponse getResponse() {
        return Optional.ofNullable(((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse())
                .orElseThrow(() -> new BizException(ErrorCode.ERROR));
    }

//    Optional<AuthPrincipal> getPrincipal() {
//        HttpServletRequest request = getRequest();
//        return Optional.ofNullable((AuthPrincipal) request.getAttribute(CommonConst.PRINCIPAL_ATTR_NAME));
//    }
}
